<template> 
    <div style="position: relative;">
        <Form ref="formValidate" :model="formValidate" :label-width="100" :rules="ruleValidate" v-if="infoOld!=null">
            <FormItem label="教师">
                <Input v-model="infoOld.teacher.teacher_name.zh" disabled></Input>          
            </FormItem>

            <FormItem label="可查看的考勤岗位" prop="position_id" v-if="stationInfo.length!=0">
                <Select placeholder="可查看的考勤岗位" v-model="formValidate.position_id" style="margin:9px 0">
                    <Option v-for="item in stationInfo" :value="item.id" :key="item.id">{{item.position_name.zh?item.position_name.zh:item.position_name.mn}}</Option>
                </Select>
            </FormItem>

            <div style="width:90%;">
                <FormItem style="width:100%;">
                    <Button type="primary" @click="handleSubmit('formValidate')">保存</Button>
                </FormItem>
            </div>
            <div style="clear:both"></div>
        </Form> 
        <Spin size="large" fix v-if="spinShow"></Spin>
    </div>
</template>
<script>
  export default {
    props: {
        infoId: {
            type: [Boolean,Number],
            default: false
        }
    },
    data() {
      return {
        stationInfo:[],//岗位数据
        spinShow:false,//input 框禁用或启用的变量

        updateUrl:`api/teacher/v1/attendancedey/edit/${this.infoId}`,
        infoUrl:`api/teacher/v1/attendancedey/find/${this.infoId}`,//请求单条数据
        infoOld:null,

        formValidate: {
            position_id:null,//部门id
            user_id:null,//教师id     
        },
        ruleValidate: {
            position_id: [
                { type:'number',required: true, message: '请选择可查看的考勤岗位', trigger: 'change' }
            ],
            user_id: [
                { type:'number',required: true, message: '请选择教师', trigger: 'change' }
            ],
        },
      }
    },
    methods: {
        
        /************************************************************
         * 
         * 根据部门id查询岗位信息
         * 
         ************************************************************/
		loadStation(value) {
            if(value!=null){
                this.spinShow=true  
                this.$http.get('api/user/v1/department/get/position/user/list?department_id='+value).then(res=> {
                    this.stationInfo = res.data 
                    this.spinShow=false
                }).catch(err => {
                    if (err.code && err.code == 422) {
                        this.$Message.error(err.message);
                    }
                    this.spinShow=false       
                })
            }
        },
        
        /************************************************************
         * 
         * 提交时
         * 
         ************************************************************/
        handleSubmit (name) {
            this.$refs[name].validate((valid) => {
                if (valid) { 
                    this.$http.put(this.updateUrl,this.formValidate).then(val=>{
                        this.$Message.success(val.message);
                        this.$emit('backList',true)  
                        this.$emit('showParent',false)
                    }).catch(err => {
                        if (err.code && err.code == 422) {
                            this.$Message.error(err.message);
                        }
                    })  
                } else {
                    this.$Message.error('表单验证失败!');
                }
            })
        },
        //单条数据请求
        infoData(){
            this.spinShow=true//输入框禁用
            this.$http.get(this.infoUrl).then(val=>{ 
                this.infoOld = JSON.parse(JSON.stringify(val.data))
                console.log(this.infoOld)
                this.formValidate.position_id=parseInt(this.infoOld.position_id)//人员
                this.formValidate.user_id=parseInt(this.infoOld.user_id)//所分配岗位
                let strs=[];
                let str=null;
                if(!this.infoOld.userdepartment_id){
                    this.infoOld.department.forEach((item,index)=>{
                        let arr=[]
                            arr.push(item.id)
                        let department_id=JSON.stringify(arr)
                        this.$http.get('api/user/v1/get/position/list?department_id='+department_id).then(res=> {
                            strs[index]=res.data.filter(ite => ite.id == this.infoOld.position_id)
                            if(strs[index].length!=0){
                                str=item.id;
                            }
                            if(str!=null){
                                this.loadStation(str)
                                return
                            }
                        }).catch(err => {
                            if (err.code && err.code == 422) {
                                this.$Message.error(err.message);
                            }
                            this.spinShow=false
                        })

                    })

                }else{
                    this.loadStation(this.infoOld.userdepartment_id)
                }
                this.spinShow=false
            }).catch(err => {
                if (err.code && err.code == 422) {
                    this.$Message.error(err.message);
                }
                this.spinShow=false
            })
        },
    },
    created() {
        this.infoData()
    },
  }
</script>
